package com.grace.cms.forum.user.controller.hand;

import com.grace.common.annotation.RepeatSubmit;
import com.grace.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.grace.cms.common.enums.CommonVisibleEnums;
import com.grace.cms.common.enums.RecordTypeEnums;
import com.grace.cms.common.model.param.WebRecordParam;
import com.grace.cms.forum.user.model.enums.RecordTableEnum;
import com.grace.cms.forum.user.service.impl.HandRecordServiceImpl;
import com.grace.cms.general.controller.CommonController;

/**
 * 默认小于一分钟为重复提交
 */
@Controller
@RequestMapping("/{themeName}/hand/record")
public class RecordHandController extends CommonController {
    @Autowired
    private HandRecordServiceImpl recordService;

    /**
     * 喜欢|点赞记录表 不能同时喜欢 讨厌
     * 
     * @param webRecordParam
     * @return
     */
    @RepeatSubmit(interval = 60000)
    @PostMapping("/{type}/addLikeRecord")
    @ResponseBody
    public AjaxResult addLikeRecord(@PathVariable("themeName") String themeName,
            @PathVariable("type") RecordTypeEnums type,@RequestBody   WebRecordParam webRecordParam) {
        setCommonRecordParam(webRecordParam, themeName, type, false);
        webRecordParam.setRecordTable(RecordTableEnum.LIKE_RECORD.getValue());
        webRecordParam.setScore(null);
        webRecordParam.setShareUrl(null);
        Integer likeVisible = recordService.selectRecordVisible(RecordTableEnum.LIKE_RECORD,
                webRecordParam.getRecordId(),
                webRecordParam.getCreateBy()),
                nastyVisible = recordService.selectRecordVisible(RecordTableEnum.NASTY_RECORD,
                        webRecordParam.getRecordId(), webRecordParam.getCreateBy());
        if (nastyVisible != null && nastyVisible.equals(CommonVisibleEnums.SHOW.ordinal())) {
            return AjaxResult.success("你已经踩过了！", "nastied");
        } else if (likeVisible != null && likeVisible.equals(CommonVisibleEnums.SHOW.ordinal())) {
            webRecordParam.setVisible(CommonVisibleEnums.HIDE.ordinal());
            recordService.updateCmsRecord(webRecordParam,type);
            return AjaxResult.success("取消点赞成功！", "cancel");
        } else if (likeVisible != null && likeVisible.equals(CommonVisibleEnums.HIDE.ordinal())) {
            webRecordParam.setVisible(CommonVisibleEnums.SHOW.ordinal());
            recordService.updateCmsRecord(webRecordParam,type);
            return AjaxResult.success("点赞成功！", "update");
        }
        // 没踩 点一次点赞 或者取消再次点赞
        else {
            webRecordParam.setVisible(CommonVisibleEnums.SHOW.ordinal());
            recordService.insertLikeRecord(webRecordParam,type);
            return AjaxResult.success("点赞成功！", "add");
        }
    }

    /**
     * 不喜欢|踩记录表 不能同时喜欢 讨厌
     * 
     * @param webRecordParam
     * @return
     */
    @RepeatSubmit(interval = 60000)
    @PostMapping("/{type}/addNastyRecord")
    @ResponseBody
    public AjaxResult addNastyRecord(@PathVariable("themeName") String themeName,@PathVariable("type") RecordTypeEnums type,  @RequestBody   WebRecordParam webRecordParam) {
        setCommonRecordParam(webRecordParam, themeName, type,false);
        webRecordParam.setRecordTable(RecordTableEnum.NASTY_RECORD.getValue());
        webRecordParam.setScore(null);
        webRecordParam.setShareUrl(null);
        Integer likeVisible = recordService.selectRecordVisible(RecordTableEnum.LIKE_RECORD,
                webRecordParam.getRecordId(),
                webRecordParam.getCreateBy()),
                nastyVisible = recordService.selectRecordVisible(RecordTableEnum.NASTY_RECORD,
                        webRecordParam.getRecordId(), webRecordParam.getCreateBy());
        // 已经点赞了
        if (likeVisible!=null && likeVisible.equals(CommonVisibleEnums.SHOW.ordinal()) ) {
            return AjaxResult.success("你已经点赞了", "liked");
        }
        // 已经踩了
        else if (nastyVisible!=null && nastyVisible.equals(CommonVisibleEnums.SHOW.ordinal())) {
            webRecordParam.setVisible(CommonVisibleEnums.HIDE.ordinal());
            recordService.updateCmsRecord(webRecordParam,type);
            return AjaxResult.success("取消踩成功！", "cancel");
        } else if (nastyVisible!=null && nastyVisible.equals( CommonVisibleEnums.HIDE.ordinal())) {
            webRecordParam.setVisible(CommonVisibleEnums.SHOW.ordinal());
            recordService.updateCmsRecord(webRecordParam,type);
            return AjaxResult.success("踩成功", "update");
        }

        else {
            webRecordParam.setVisible(CommonVisibleEnums.SHOW.ordinal());
            recordService.insertNastyRecord(webRecordParam,type);
            return AjaxResult.success("踩成功！", "add");
        }
    }

    /**
     * 添加评分
     * 
     * @param webRecordParam
     * @return
     */
    @RepeatSubmit(interval = 60000)
    @PostMapping("/{type}/addScoreRecord")
    @ResponseBody
    public AjaxResult addScoreRecord(@PathVariable("themeName") String themeName,
            @PathVariable("type") RecordTypeEnums type, @RequestBody   WebRecordParam webRecordParam) {
        Integer scoreVisible = recordService.selectRecordVisible(RecordTableEnum.SCORE_RECORD,
                type.getPrefix(webRecordParam.getRecordId()),
                webRecordParam.getCreateBy());
        webRecordParam.setShareUrl(null);
        webRecordParam.setRecordTable(RecordTableEnum.SCORE_RECORD.getValue());
        if (scoreVisible!=null && scoreVisible == CommonVisibleEnums.SHOW.ordinal()) {
            webRecordParam.setVisible(CommonVisibleEnums.HIDE.ordinal());
            recordService.updateCmsRecord(webRecordParam,type);
            return AjaxResult.success("取消评分成功", "cancel");
        } else if (scoreVisible!=null && scoreVisible == CommonVisibleEnums.HIDE.ordinal()) {
            webRecordParam.setVisible(CommonVisibleEnums.SHOW.ordinal());
            recordService.updateCmsRecord(webRecordParam,type);
            return AjaxResult.success("更新评分成功", "update");
        } else {
            setCommonRecordParam(webRecordParam, themeName, type, false);
            recordService.insertScoreRecord(webRecordParam,type);
            return AjaxResult.success("添加评分成功", "add");
        }
    }

    /**
     * 文章分享
     * 
     * @param webRecordParam
     * @return
     */
    @RepeatSubmit(interval = 60000)
    @PostMapping("/{type}/addShareRecord")
    @ResponseBody
    public AjaxResult addShareRecord(@PathVariable("themeName") String themeName,
            @PathVariable("type") RecordTypeEnums type, @RequestBody   WebRecordParam webRecordParam) {
        setCommonRecordParam(webRecordParam, themeName, type, false);
        webRecordParam.setScore(null);
        webRecordParam.setRecordTable(RecordTableEnum.SHARE_RECORD.getValue());
        return AjaxResult.success(recordService.insertShareRecord(webRecordParam,type));
    }

    /**
     * 添加收藏
     * 
     * @param webRecordParam
     * @return
     */
    @RepeatSubmit(interval = 60000)
    @PostMapping("/{type}/addCollectRecord")
    @ResponseBody
    public AjaxResult addCollectRecord(@PathVariable("themeName") String themeName,
            @PathVariable("type") RecordTypeEnums type, @RequestBody   WebRecordParam webRecordParam) {
        setCommonRecordParam(webRecordParam, themeName, type, false);
        webRecordParam.setRecordTable(RecordTableEnum.COLLECT_RECORD.getValue());
        webRecordParam.setScore(null);
        webRecordParam.setShareUrl(null);
        Integer collectVisible = recordService.selectRecordVisible(RecordTableEnum.COLLECT_RECORD,
                type.getPrefix(webRecordParam.getRecordId()),
                webRecordParam.getCreateBy());
        if (collectVisible != null && collectVisible == CommonVisibleEnums.SHOW.ordinal()) {
            webRecordParam.setVisible(CommonVisibleEnums.HIDE.ordinal());
            recordService.updateCmsRecord(webRecordParam,type);
            return AjaxResult.success("取消收藏成功", "cancel");
        } else if (collectVisible != null && collectVisible == CommonVisibleEnums.HIDE.ordinal()) {
            webRecordParam.setVisible(CommonVisibleEnums.SHOW.ordinal());
            recordService.updateCmsRecord(webRecordParam,type);
            return AjaxResult.success("添加收藏成功", "update");
        } else {

            recordService.insertCollectRecord(webRecordParam,type);
            return AjaxResult.success("添加收藏成功", "add");
        }
    }

}
